home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 108 / MacAddict108.iso / Software / Internet & Communication / WordPress 1.5.1.dmg / wordpress / wp-content / plugins / textile1.php < prev   
Encoding:
PHP Script  |  2005-03-10  |  11.0 KB  |  390 lines

  1. <?php
  2. /*
  3. Plugin Name: Textile 1
  4. Version: 1.0
  5. Plugin URI: http://www.huddledmasses.org/
  6. Description: This is a simple wrapper for <a href="http://textism.com/?wp">Dean Allen's</a> Humane Web Text Generator, also known as <a href="http://www.textism.com/tools/textile/">Textile</a>. If you use this plugin you should disable Textile 2 and Markdown, as they don't play well together.
  7. Author: Dean Allen
  8. Author URI: http://www.textism.com/?wp
  9. */
  10.  
  11. /*
  12.  
  13. This is Textile
  14. A Humane Web Text Generator
  15.  
  16. Version 1.0
  17. 21 Feb, 2003
  18.  
  19. Copyright (c) 2003, Dean Allen, www.textism.com
  20. All rights reserved.
  21.  
  22. _______
  23. LICENSE
  24.  
  25. Redistribution and use in source and binary forms, with or without
  26. modification, are permitted provided that the following conditions are met:
  27.  
  28. * Redistributions of source code must retain the above copyright notice,
  29.   this list of conditions and the following disclaimer.
  30.  
  31. * Redistributions in binary form must reproduce the above copyright notice,
  32.   this list of conditions and the following disclaimer in the documentation
  33.   and/or other materials provided with the distribution.
  34.  
  35. * Neither the name Textile nor the names of its contributors may be used to
  36.   endorse or promote products derived from this software without specific
  37.   prior written permission.
  38.  
  39. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  40. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  42. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  43. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  44. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  45. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  46. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  47. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  48. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  49. POSSIBILITY OF SUCH DAMAGE.
  50.  
  51. */
  52.  
  53.  
  54.     function textile($text) {
  55.     $text = stripslashes($text);
  56.  
  57.     $text = preg_replace("/&(?![#a-zA-Z0-9]+;)/","x%x%",$text);
  58.  
  59.     if (function_exists('mb_encode_numericentity')) {
  60.         $text = encode_high($text);
  61.     } else {
  62.         $text = htmlentities($text,ENT_NOQUOTES,"utf-8");
  63.     }
  64.  
  65.     $text = str_replace(array(">", "<", "&"), array(">", "<", "&"), $text);
  66.  
  67.     $text = str_replace("\r\n", "\n", $text);
  68.  
  69.     $text = str_replace("\t", "", $text);
  70.  
  71.     $text = preg_split("/\n/",$text);
  72.     foreach($text as $line){
  73.         $line = rtrim($line);
  74.         $lineout[] = $line;
  75.     }
  76.  
  77.     $text = implode("\n",$lineout);
  78.  
  79.     $text = preg_replace('/(^|\s)==(.*)==(\s|$)?/msU','$1<notextile>$2</notextile>$3',$text);
  80.  
  81.     $text = preg_replace('/!([^\s\(=]+)\s?(?:\(([^\)]+)\))?!(\s)?/mU','<img src="$1" alt="$2" border="0" />$3',$text);
  82.  
  83.     $text = preg_replace('/(<img.+ \/>):(\S+)(\s)/U','<a href="$2">$1</a>$3',$text);
  84.  
  85.     $text = preg_replace(
  86.         '/
  87.         ([\s[{(]|[[:punct:]])?        # 1 optional space or brackets before
  88.         "                            #   starting "
  89.         ([^"\(]+)                    # 2 text of link
  90.         \s?                            #   opt space
  91.         (?:\(([^\(]*)\))?            # 3 opt title attribute in parenths
  92.         ":                            #   dividing ":
  93.         (\S+\b)                        # 4 suppose this is the url
  94.         (\/)?                        # 5 opt trailing slash
  95.         ([^[:alnum:]\/;]*)            # 6 opt punctuation after the url
  96.         (\s|$)                        # 7 either white space or end of string
  97.         /x',
  98.         '$1<a href="$4$5" title="$3">$2</a>$6$7',$text);
  99.  
  100.     $qtags = array(
  101.         '\*\*'=>'b',
  102.         '\*'=>'strong',
  103.         '\?\?'=>'cite',
  104.         '-'=>'del',
  105.         '\+'=>'ins',
  106.         '~'=>'sub',
  107.         '@'=>'code');
  108.  
  109.     foreach($qtags as $f=>$r){
  110.         $text = preg_replace(
  111.             '/(^|\s|>)'.$f.'\b(.+)\b([[:punct:]]*)'.$f.'([[:punct:]]{0,2})(\s|$)?/mU',
  112.             '$1<'.$r.'>$2$3</'.$r.'>$4$5',
  113.             $text);
  114.     }
  115.  
  116.     $text = preg_replace('/(^|\s)__(.*)__([[:punct:]]{0,2})(\s|$)?/mU','$1<i>$2</i>$3$4',$text);
  117.     $text = preg_replace('/(^|\s)_(.*)_([[:punct:]]{0,2})(\s|$)?/mU','$1<em>$2</em>$3$4',$text);
  118.  
  119.     $text = preg_replace('/\^(.*)\^/mU','<sup>$1</sup>',$text);
  120.  
  121.     $text = preg_replace('/"$/',"\" ", $text);
  122.     $glyph_search = array(
  123.         '/([^\s[{(>])?\'(?(1)|(?=\s|s\b))/',                    # single closing
  124.         '/\'/',                                                    # single opening
  125.         '/([^\s[{(])?"(?(1)|(?=\s))/',                            # double closing
  126.         '/"/',                                                    # double opening
  127.         '/\b( )?\.{3}/',                                        # ellipsis
  128.         '/\b([A-Z][A-Z0-9]{2,})\b(?:[(]([^)]*)[)])/',            # 3+ uppercase acronym
  129.         '/(^|[^"][>\s])([A-Z][A-Z0-9 ]{2,})([^<a-z0-9]|$)/',    # 3+ uppercase caps
  130.         '/\s?--\s?/',                                            # em dash
  131.         '/\s-\s/',                                                # en dash
  132.         '/(\d+) ?x ?(\d+)/',                                    # dimension sign
  133.         '/\b ?[([]TM[])]/i',                                    # trademark
  134.         '/\b ?[([]R[])]/i',                                        # registered
  135.         '/\b ?[([]C[])]/i');                                    # copyright
  136.  
  137.     $glyph_replace = array(
  138.         '$1’$2',                            # single closing
  139.         '‘',                                # single opening
  140.         '$1”',                            # double closing
  141.         '“',                                # double opening
  142.         '$1…',                            # ellipsis
  143.         '<acronym title="$2">$1</acronym>',        # 3+ uppercase acronym
  144.         '$1<span class="caps">$2</span>$3',        # 3+ uppercase caps
  145.         '—',                                # em dash
  146.         ' – ',                            # en dash
  147.         '$1×$2',                            # dimension sign
  148.         '™',                                # trademark
  149.         '®',                                # registered
  150.         '©');                                # copyright
  151.  
  152.     $codepre = false;
  153.  
  154.     if(!preg_match("/<.*>/",$text)){
  155.         $text = preg_replace($glyph_search,$glyph_replace,$text);
  156.     } else {
  157.  
  158.         $text = preg_split("/(<.*>)/U",$text,-1,PREG_SPLIT_DELIM_CAPTURE);
  159.             foreach($text as $line){
  160.  
  161.                     # matches are off if we're between <code>, <pre> etc.
  162.                 if(preg_match('/<(code|pre|kbd|notextile)>/i',$line)){$codepre = true; }
  163.                 if(preg_match('/<\/(code|pre|kbd|notextile)>/i',$line)){$codepre = false; }
  164.  
  165.                 if(!preg_match("/<.*>/",$line) && $codepre == false){
  166.                     $line = preg_replace($glyph_search,$glyph_replace,$line);
  167.                 }
  168.  
  169.                 # convert htmlspecial if between <code>
  170.                 if ($codepre == true){
  171.                     $line = htmlspecialchars($line,ENT_NOQUOTES,"UTF-8");
  172.                     $line = str_replace("<pre>","<pre>",$line);
  173.                     $line = str_replace("<code>","<code>",$line);
  174.                     $line = str_replace("<notextile>","<notextile>",$line);
  175.                     $line = str_replace("<kbd>","<kbd>",$line);
  176.                 }
  177.  
  178.                 # each line gets pushed to a new array
  179.             $glyph_out[] = $line;
  180.         }
  181.             # $text is now the new array, cast to a string
  182.         $text = implode('',$glyph_out);
  183.     }
  184.  
  185.     $text = preg_replace("/(\S)(_*)([[:punct:]]*) *\n([^#*\s])/", "$1$2$3<br />$4", $text);
  186.  
  187.     $text = str_replace("l><br />", "l>\n", $text);
  188.  
  189.     $text = preg_split("/\n/",$text);
  190.  
  191.     array_push($text," ");
  192.  
  193.             $list = '';
  194.             $pre = false;
  195.  
  196.             $block_find = array(
  197.                 '/^\s?\*\s(.*)/',                        # bulleted list *
  198.                 '/^\s?#\s(.*)/',                        # numeric list #
  199.                 '/^bq\. (.*)/',                            # blockquote bq.
  200.                 '/^h(\d)\(([[:alnum:]]+)\)\.\s(.*)/',    # header hn(class).  w/ css class
  201.                 '/^h(\d)\. (.*)/',                        # plain header hn.
  202.                 '/^p\(([[:alnum:]]+)\)\.\s(.*)/',        # para p(class).  w/ css class
  203.                 '/^p\. (.*)/i',                             # plain paragraph
  204.                 '/^([^\t ]+.*)/i'                        # remaining plain paragraph
  205.                 );
  206.  
  207.             $block_replace = array(
  208.                 "\t\t<li>$1</li>",
  209.                 "\t\t\t<li>$1</li>",
  210.                 "\t<blockquote>$1</blockquote>",
  211.                 "\t<h$1 class=\"$2\">$3</h$1>$4",
  212.                 "\t<h$1>$2</h$1>$3",
  213.                 "\t<p class=\"$1\">$2</p>$3",
  214.                 "\t<p>$1</p>",
  215.                 "\t<p>$1</p>$2"
  216.                 );
  217.  
  218.     foreach($text as $line){
  219.  
  220.             if(preg_match('/<pre>/i',$line)){$pre = true; }
  221.  
  222.             if ($pre == false){
  223.                 $line = preg_replace($block_find,$block_replace,$line);
  224.             }
  225.  
  226.             if ($pre == true){
  227.                 $line = str_replace("<br />","\n",$line);
  228.             }
  229.                 if(preg_match('/<\/pre>/i',$line)){$pre = false; }
  230.  
  231.             if ($list == '' && preg_match('/^\t\t<li>/',$line)){
  232.                     $list = "ul";
  233.                 $line = preg_replace('/^(\t\t<li>.*)/',"\t<ul>\n$1",$line);
  234.  
  235.             } else if ($list == '' && preg_match('/^\t\t\t<li>/',$line)){
  236.                     $list = "ol";
  237.                 $line = preg_replace('/^\t(\t\t<li>.*)/',"\t<ol>\n$1",$line);
  238.  
  239.             # at the end of a ul
  240.             } else if ($list == 'ul' && !preg_match('/^\t\t<li>/',$line)){
  241.                     $list = '';
  242.                 $line = preg_replace('/^(.*)$/',"\t</ul>\n$1",$line);
  243.  
  244.             # at the end of a ol
  245.             } else if ($list == 'ol' && !preg_match('/^\t\t\t<li>/',$line)){
  246.                     $list = '';
  247.                 $line = preg_replace('/^(.*)$/',"\t</ol>\n$1",$line);
  248.             }
  249.  
  250.             $block_out[] = $line;
  251.     }
  252.     $text = implode("\n",$block_out);
  253.     $text = preg_replace('/<\/?notextile>/', "",$text);
  254.     $text = str_replace("x%x%","&",$text);
  255.     $text = str_replace("<br />","<br />\n",$text);
  256.     return $text;
  257. }
  258.  
  259. function callback_url($text,$title='',$url) {
  260.         $out = 'a href="'.$url.'"';
  261.         $out.=($title!='')?' title="'.$title.'"':'';
  262.         $out.='>$text</a>';
  263.         return $out;
  264.     }
  265.  
  266.  
  267.  
  268. function textile_popup_help($name,$helpvar,$windowW,$windowH) {
  269.     $out = $name;
  270.     $out .= ' <a target="_blank" href="http://www.textpattern.com/help/?item='.$helpvar.'"';
  271.     $out .= ' onclick="window.open(this.href, \'popupwindow\', \'width='.$windowW.',height='.$windowH.',scrollbars,resizable\'); return false;" style="color:blue;background-color:#ddd">?</a><br />';
  272.     print $out;
  273. }
  274.  
  275.  
  276. function encode_high($text) {
  277.     $cmap = cmap();
  278.     return mb_encode_numericentity($text, $cmap, "UTF-8");
  279. }
  280.  
  281.  
  282. function decode_high($text) {
  283.     $cmap = cmap();
  284.     return mb_decode_numericentity($text, $cmap, "UTF-8");
  285. }
  286.  
  287.  
  288. function cmap() {
  289.  
  290.     $f = 0xffff;
  291.  
  292.     $cmap = array(
  293.      160,  255,  0, $f,
  294.      402,  402,  0, $f,
  295.      913,  929,  0, $f,
  296.      931,  937,  0, $f,
  297.      945,  969,  0, $f,
  298.      977,  978,  0, $f,
  299.      982,  982,  0, $f,
  300.      8226, 8226, 0, $f,
  301.      8230, 8230, 0, $f,
  302.      8242, 8243, 0, $f,
  303.      8254, 8254, 0, $f,
  304.      8260, 8260, 0, $f,
  305.      8465, 8465, 0, $f,
  306.      8472, 8472, 0, $f,
  307.      8476, 8476, 0, $f,
  308.      8482, 8482, 0, $f,
  309.      8501, 8501, 0, $f,
  310.      8592, 8596, 0, $f,
  311.      8629, 8629, 0, $f,
  312.      8656, 8660, 0, $f,
  313.      8704, 8704, 0, $f,
  314.      8706, 8707, 0, $f,
  315.      8709, 8709, 0, $f,
  316.      8711, 8713, 0, $f,
  317.      8715, 8715, 0, $f,
  318.      8719, 8719, 0, $f,
  319.      8721, 8722, 0, $f,
  320.      8727, 8727, 0, $f,
  321.      8730, 8730, 0, $f,
  322.      8733, 8734, 0, $f,
  323.      8736, 8736, 0, $f,
  324.      8743, 8747, 0, $f,
  325.      8756, 8756, 0, $f,
  326.      8764, 8764, 0, $f,
  327.      8773, 8773, 0, $f,
  328.      8776, 8776, 0, $f,
  329.      8800, 8801, 0, $f,
  330.      8804, 8805, 0, $f,
  331.      8834, 8836, 0, $f,
  332.      8838, 8839, 0, $f,
  333.      8853, 8853, 0, $f,
  334.      8855, 8855, 0, $f,
  335.      8869, 8869, 0, $f,
  336.      8901, 8901, 0, $f,
  337.      8968, 8971, 0, $f,
  338.      9001, 9002, 0, $f,
  339.      9674, 9674, 0, $f,
  340.      9824, 9824, 0, $f,
  341.      9827, 9827, 0, $f,
  342.      9829, 9830, 0, $f,
  343.      338,  339,  0, $f,
  344.      352,  353,  0, $f,
  345.      376,  376,  0, $f,
  346.      710,  710,  0, $f,
  347.      732,  732,  0, $f,
  348.      8194, 8195, 0, $f,
  349.      8201, 8201, 0, $f,
  350.      8204, 8207, 0, $f,
  351.      8211, 8212, 0, $f,
  352.      8216, 8218, 0, $f,
  353.      8218, 8218, 0, $f,
  354.      8220, 8222, 0, $f,
  355.      8224, 8225, 0, $f,
  356.      8240, 8240, 0, $f,
  357.      8249, 8250, 0, $f,
  358.      8364, 8364, 0, $f
  359.      );
  360.  
  361.     return $cmap;
  362. }
  363.  
  364.  
  365. function linkit($text,$title,$url){
  366.     $url = preg_replace("/&(?!amp;)/","&",$url);
  367.     $out = '<a href="'.$url;
  368.     if ($title!='') {
  369.         $title = trim($title);
  370.         $out .= ' title="'.$title;
  371.     }
  372.     $out .= '>'.$text.'</a>';
  373.  
  374.     return $out;
  375. }
  376.  
  377. // And now for the filters
  378. remove_filter('the_content', 'wpautop');
  379. remove_filter('the_excerpt', 'wpautop');
  380. remove_filter('comment_text', 'wpautop');
  381.  
  382. remove_filter('the_content', 'wptexturize');
  383. remove_filter('the_excerpt', 'wptexturize');
  384. remove_filter('comment_text', 'wptexturize');
  385.  
  386. add_filter('the_content', 'textile', 6);
  387. add_filter('the_excerpt', 'textile', 6);
  388. add_filter('comment_text', 'textile', 6);
  389.  
  390. ?>